/*
**    Copyright (C) 2003-2011 Institute for Systems Biology
**                            Seattle, Washington, USA.
**
**    This library is free software; you can redistribute it and/or
**    modify it under the terms of the GNU Lesser General Public
**    License as published by the Free Software Foundation; either
**    version 3 of the License, or (at your option) any later version.
**
**    This library is distributed in the hope that it will be useful,
**    but WITHOUT ANY WARRANTY; without even the implied warranty of
**    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
**    Lesser General Public License for more details.
**
**    You should have received a copy of the GNU Lesser General Public
**    License along with this library; if not, write to the Free Software
**    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307  USA
*/

package org.systemsbiology.iteration;

import java.util.Iterator;

/**
 * Author: anorberg
 * Created on: Nov 10, 2010
 */

/**
 * Tiny helper class to let me clean up for(:) syntax on APIs that return iterators but are not from Iterable objects,
 * usually because the object has more than one iterator or the iterator method requires parameters.
 *
 * Beware that the iterator() method here neither clones nor resets the iterator, so creating multiple iterators
 * from this class is not safe. This is a one-shot disposable shell specifically for use in for(:) headers!
 * @param <T> Value type of the underlying iterator.
 */
public final class IterableFromIterator<T> implements Iterable<T>{
    private Iterator<? extends T> underlyingIterator = null;
    public IterableFromIterator(Iterator<? extends T> it){
        if(it == null){throw new NullPointerException("IterableFromIterator needs its iterator specified");}
        underlyingIterator = it;
    }
    /**
     * Implements contract of Iterable by returning the Iterator this had in the firstp lace.
     */
    public Iterator<T> iterator(){
        return new IteratorConverter<T, T>(underlyingIterator){
			@Override
			protected T convert(T next) {
				return next;
			}
        	
        };
    }
}
